Repository navigation
Mark only fingerprinted static files as immutable - #3127
Merged
Merged
Conversation
We previously served every file in public/ with a 1-year "immutable" Cache-Control. That's correct for fingerprinted assets, whose URL changes whenever their content does. However, it is *not* correct for files with fixed URLs such as 404.html, 422.html, 500.html, and the Google verifier. Once cached, fixes to those can't reach browsers or the CDN. It also hid a bug: public/favicon.ico was an empty file left over from the 2015 Rails scaffold, so anything requesting /favicon.ico directly got zero bytes, cached as immutable for a year. To fix this and make the mistake hard to repeat: * public/ files now get a 1-day default (FingerprintedAssetCacheMiddleware::DEFAULT_HEADERS), shared by the production and test configs. * New middleware wrapping ActionDispatch::Static upgrades a response to immutable only if its URL has a Sprockets SHA-256 fingerprint. A new file can't get the long lifetime unless its URL changes with its content. * /favicon.ico now 301-redirects to the fingerprinted icon from app/assets/images/favicon.ico. The redirect is cached for 1 day, the icon for a year. We removed the empty public/favicon.ico. * StaticPagesController lists its locale-free actions once. Tests cover the fingerprint pattern (including near misses), the middleware, real static responses, and the favicon redirect. After deploying, purge Fastly; it's caching the empty /favicon.ico and the error pages with a 1-year lifetime. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
CI failed the test CdnCachingTest#test_static_files_are_immutable_only_when_fingerprinted: the fingerprinted favicon got Sprockets' "public, max-age=31536000, immutable" instead of our IMMUTABLE value. CI runs the tests without precompiled assets, so Sprockets' server, mounted at /assets, serves them. That mount strips "/assets" from env['PATH_INFO'], and FingerprintedAssetCacheMiddleware read PATH_INFO *after* calling the app, so the path no longer matched the fingerprint pattern. Locally the test passed only because public/assets existed, so ActionDispatch::Static served the file without touching PATH_INFO. We now check the path before calling the app. A new unit test uses an app that rewrites PATH_INFO, so this fails regardless of whether assets are precompiled. I also ran the full suite both with and without public/assets. That should prevent future similar problems. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
Add comments to clarify when we use *Rack* env vs. *process* env. We're using standard conventions, but those standard conventions can be a little confusing; better to make it clear what we're doing. Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3127 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 89 90 +1
Lines 4797 4817 +20
=========================================
+ Hits 4797 4817 +20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We previously served every file in public/ with a 1-year "immutable" Cache-Control. That's correct for fingerprinted assets, whose URL changes whenever their content does. However, it is not correct for files with fixed URLs such as 404.html, 422.html, 500.html, and the Google verifier. Once cached, fixes to those can't reach browsers or the CDN.
It also hid a bug: public/favicon.ico was an empty file left over from the 2015 Rails scaffold, so anything requesting /favicon.ico directly got zero bytes, cached as immutable for a year.
To fix this and make the mistake hard to repeat:
Tests cover the fingerprint pattern (including near misses), the middleware, real static responses, and the favicon redirect.
After deploying, purge Fastly; it's caching the empty /favicon.ico and the error pages with a 1-year lifetime.
Assisted-by: Claude:claude-opus-5-5